package com.csj.mvvmtest.ui.demo;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.csj.mvvmtest.R;
import com.csj.mvvmtest.widget.heart.HeartView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class HeartAct extends AppCompatActivity {

    HeartView heartView;
    private TextView tv_text;
    private TextView tv_text_1;
    private TextView tv_text_2;
    private int clo = 0;

    private RelativeLayout countDown;
    // 倒计时
    private TextView daysTv, hoursTv, minutesTv, secondsTv;
    private long mDay = 552;
    private long mHour = 15;
    private long mMin = 37;
    private long mSecond = 00;// 天 ,小时,分钟,秒
    private boolean isRun = true;

    private Handler timeHandler = new Handler(getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                computeTime();
                daysTv.setText(mDay + "");
                hoursTv.setText(mHour + "");
                minutesTv.setText(mMin + "");
                secondsTv.setText(mSecond + "");
                if (mDay == 0 && mHour == 0 && mMin == 0 && mSecond == 0) {
                    countDown.setVisibility(View.GONE);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heart);
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d1 = format.parse("2020-01-23 00:00:00");//后的时间
            Long diff = System.currentTimeMillis() - d1.getTime(); //两时间差，精确到毫秒
            mDay = diff / (1000 * 60 * 60 * 24);   //以天数为单位取整
            mHour = (diff / (60 * 60 * 1000) - mDay * 24);    //以小时为单位取整
            mMin = ((diff / (60 * 1000)) - mDay * 24 * 60 - mHour * 60); //以分钟为单位取整
            mSecond = (diff / 1000 - mDay * 24 * 60 * 60 - mHour * 60 * 60 - mMin * 60);//秒
        } catch (Exception e) {
            e.printStackTrace();
        }

        heartView = (HeartView) findViewById(R.id.surfaceView);
        tv_text = (TextView) findViewById(R.id.myword);
        tv_text_1 = (TextView) findViewById(R.id.myword_1);
        tv_text_2 = (TextView) findViewById(R.id.myword_2);
        shark();

        countDown = (RelativeLayout) findViewById(R.id.countdown_layout);
        daysTv = (TextView) findViewById(R.id.days_tv);
        hoursTv = (TextView) findViewById(R.id.hours_tv);
        minutesTv = (TextView) findViewById(R.id.minutes_tv);
        secondsTv = (TextView) findViewById(R.id.seconds_tv);
        daysTv.setText(mDay + "");
        hoursTv.setText(mHour + "");
        minutesTv.setText(mMin + "");
        secondsTv.setText(mSecond + "");
        startRun();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        heartView.reDraw();
        return super.onTouchEvent(event);
    }

    public void reDraw(View v) {

        heartView.reDraw();
    }

    Timer timer;
    TimerTask taskcc;

    private void shark() {
        timer = new Timer();
        taskcc = new TimerTask() {
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        int cl = Color.BLUE;
                        switch (clo) {
                            case 0:
                                cl = Color.TRANSPARENT;
                                break;
                            case 1:
                                cl = Color.parseColor("#FFCCCC");
                                break;
                            case 2:
                                cl = Color.RED;
                                break;
                            case 3:
                                cl = Color.parseColor("#FF6600");
                                break;
                            case 4:
                                cl = Color.YELLOW;
                                break;
                            case 5:
                                cl = Color.GREEN;
                                break;
                            case 6:
                                cl = Color.BLUE;
                                break;
                            case 7:
                                cl = Color.CYAN;
                                break;
                            case 8:
                                cl = Color.parseColor("#CC33FF");
                                break;
                            case 9:
                                cl = Color.GRAY;
                                break;
                            case 10:
                                cl = Color.MAGENTA;
                                break;
                        }
                        if (clo == 11) {
                            clo = 0;
                        } else {
                            clo++;
                        }
                        tv_text.setTextColor(cl);
                        tv_text_1.setTextColor(cl);
                        tv_text_2.setTextColor(cl);
                    }
                });
            }
        };
        timer.schedule(taskcc, 1, 1500);  //第二个参数分别是delay（多长时间后执行），第三个参数是：duration（执行间隔）单位为：ms
    }

    Thread thread;

    /**
     * 开启计时
     */
    private void startRun() {
        thread = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (isRun) {
                    try {
                        Thread.sleep(1000); // sleep 1000ms
                        Message message = Message.obtain();
                        message.what = 1;
                        timeHandler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
    }

    /**
     * 倒计时计算
     */
    private void computeTime() {
        mSecond++;
        if (mSecond > 60) {
            mSecond = 0;
            mMin++;
            if (mMin > 60) {
                mMin = 0;
                mHour++;
                if (mHour > 24) {
                    mHour = 0;
                    // 倒计时结束
                    mDay++;
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isRun = false;
        thread.interrupt();
        taskcc.cancel();
        timer.cancel();
        timeHandler.removeCallbacksAndMessages(null);
    }
}
